package com.tywj.module.h5.base

import android.os.Bundle
import android.view.KeyEvent
import android.view.KeyEvent.KEYCODE_BACK
import android.view.View
import android.widget.ProgressBar
import com.alibaba.android.arouter.facade.annotation.Route
import com.tywj.lib.core.common.util.BarUtil
import com.tywj.lib.core.common.util.LogUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.route.RouteParam
import com.tywj.module.h5.R
import com.tywj.module.h5.client.MyWebChromeClient
import com.tywj.module.h5.route.H5Route
import com.tywj.module.interfaces.IH5View
import com.tywj.panda.customer.common.view.base.MyBaseActivity
import kotlinx.android.synthetic.main.h5_common_activity.*

/**
 * 浏览器 webview
 */
@Route(path = H5Route.H5_ACTIVITY)
class MyWebViewActivity : MyBaseActivity(), IH5View {
    override fun getLayoutID(): Int = R.layout.h5_common_activity

    companion object {
        const val PARAMS_URL = "url"
        const val PARAMS_TITLE = "title"
    }

    private var url = ""

    private lateinit var webChromeClient: MyWebChromeClient

    private lateinit var progressBar: ProgressBar

    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        url = RouteParam(this).getString(PARAMS_URL)
        title = RouteParam(this).getString(PARAMS_TITLE)

        titleLayout.getTitleTextView().text = title
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        BarUtil.setStatusBarColor(this, ResourcesUtil.getColor(R.color.common_white))
        BarUtil.setStatusBarLightMode(this.window, true)

        webChromeClient = MyWebChromeClient(this)
        x5WebView.webChromeClient = webChromeClient

        progressBar = getProgressBar()
    }

    override fun initListener() {
        super.initListener()

        titleLayout.setOnBackClickListener {
            // 如果用户按下的返回键且网页有历史记录，则返回上一个H5页面，否则返回上一个Native界面
            if (x5WebView?.canGoBack() == true) {
                x5WebView?.goBack()
                titleLayout.setInterruptBack(true)
            } else {
                titleLayout.setInterruptBack(false)
            }
        }
    }

    override fun loadDataOnCreate() {
        super.loadDataOnCreate()
        LogUtil.d("当前H5地址：$url")
        x5WebView.loadUrl(url)
    }

    private fun getProgressBar(): ProgressBar = findViewById(R.id.progressBar)

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        if (keyCode == KEYCODE_BACK && x5WebView.canGoBack()) {
            x5WebView.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }


    override fun onProgressChanged(progress: Int) {
        if (progress == 100) {
            if (progressBar.visibility == View.VISIBLE) {
                progressBar.visibility = View.GONE
            }
        } else {
            if (progressBar.visibility == View.GONE) {
                progressBar.visibility = View.VISIBLE
            }
            progressBar.progress = progress
        }
    }
    //TODO：网络异常 换成统一的刷新机制和界面
}